package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.AddContactsActivity;
import com.zzptc.zzsgz.bdsjws.activities.OneKeySOSActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SosAdapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzsgz on 2017/5/27.
 */

public class SecondFragment extends BaseFragmentbase {


    @BindView(R.id.et_add)
    EditText etAdd;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.btn_yes)
    Button btnYes;
    Unbinder unbinder;
    @BindView(R.id.rv_sos_tel)
    RecyclerView rvSosTel;
    Unbinder unbinder1;
    Unbinder unbinder2;
    private SosAdapter sosAdapter;
    List<Contact> listSos = MyApplication.goSosList();

    @Override
    public void initView() {
        sosAdapter = new SosAdapter(listSos);
        rvSosTel.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvSosTel.setAdapter(sosAdapter);
        etAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                btnYes.setVisibility(View.VISIBLE);
                imgAdd.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public int getXML() {
        return R.layout.second_fragment;
    }


    @OnClick(R.id.img_add)
    public void onViewClicked() {
        //Toast.makeText(getActivity(), "======", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(), AddContactsActivity.class));
    }


    @OnClick(R.id.btn_yes)
    public void onYesClicked() {
        //先判断电话号码的长度  并且要获取电话号码
        String tel = etAdd.getText().toString().trim();

        if (tel.length() == 11 && tel.matches("^1[3578]\\d{9}")) {
            //放入集合中

            Contact contact = new Contact();
            contact.setPhonenum(tel);

            if (listSos.size() < 3) {

                listSos.add(contact);
            } else {
                Toast.makeText(getActivity(), "已经有三人", Toast.LENGTH_SHORT).show();
            }


            //适配器


        }

        //判断电话号码的长度

        etAdd.setText("");
        btnYes.setVisibility(View.GONE);
        imgAdd.setVisibility(View.VISIBLE);
        sosAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sosAdapter != null) {
            sosAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder2 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder2.unbind();
    }

    @OnClick(R.id.but_sos_wancheng)
    public void onViewClickeds() {


        SharedPreferences sharedPreferences= MyUtil.getSharedPre();

        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("SOS","上课测试");
        editor.commit();

        for(int i=0;i<MyApplication.goSosList().size();i++){
            Contact c=MyApplication.goSosList().get(i);
            //if (MyUtil.getAllContacts().contains(c))
        }
        //跳转
        if (MyApplication.goSosList().size()==0){
            Toast.makeText(getActivity(), "请选择求救联系人", Toast.LENGTH_SHORT).show();
        }
        else {
        if(getActivity() instanceof OneKeySOSActivity){
//调用Activity中的一个方法    到第三个个Fragment
            ((OneKeySOSActivity) getActivity()).toThirdFragment();
        }
    }
    }
}
